package cn.shujuhai.cms.controller.group;


import cn.shujuhai.cms.domain.Group;
import cn.shujuhai.cms.service.GroupService;
import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.PageRequest;
import cn.shujuhai.common.pluging.data.ResultVo;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: NiuYaHu
 * @Date: 2024/12/25 10:12
 * @Description:
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/cms/group")
public class GroupController {
    final GroupService groupService;

    /**
     * 查询收件人分组列表
     *
     * @param groupName   收件人分组名称
     * @param pageRequest
     * @return 查询的分组列表
     */
    @SysLog(value = "收件人分组列表查询", operationType = "查询", module = "收件人分组模块")
    @GetMapping("/list")
    public ResultVo<PageInfo<Group>> getGroupList
    (@Validated String groupName, @Valid PageRequest pageRequest) {
        return this.groupService.getGroupPageInfo(groupName, pageRequest.getPageNum(), pageRequest.getPageSize());
    }

    /**
     * 新增分组
     *
     * @param group 新增收件人分组对象
     * @return 新增收件人分组对象
     */
    @SysLog(value = "新增收件人分组", operationType = "新增", module = "收件人分组模块")
    @PostMapping("/add")
    public ResultVo<?> save(@RequestBody @Validated Group group) {
        return this.groupService.add(group);
    }

    /**
     * 收件人分组编辑
     *
     * @param group 编辑收件人分组对象
     * @return 更新收件人分组对象
     */
    @SysLog(value = "收件人分组编辑", operationType = "编辑", module = "收件人分组模块")
    @PostMapping("/update")
    public ResultVo<Group> update(@RequestBody @Validated Group group) {
        return this.groupService.edit(group);
    }

    /**
     * 删除收件人分组
     *
     * @param id 收件人分组Id
     * @return 删除响应结果
     */
    @SysLog(value = "删除收件人分组", operationType = "删除", module = "收件人分组模块")
    @PostMapping("/delete/{id}")
    public ResultVo<Group> delete(@PathVariable Integer id) {
        return this.groupService.removeId(id);
    }

    /**
     * 获取收件人分组列表
     *
     * @return 收件人分组列表
     */
    @GetMapping("/getGroupList")
    public ResultVo<?> getGroupList() {
        return ResultVo.success(this.groupService.list());
    }


}
